package com.example.commoditymanagementsystemspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.commoditymanagementsystemspringboot.dao.UserDao;
import com.example.commoditymanagementsystemspringboot.po.PageBean;
import com.example.commoditymanagementsystemspringboot.po.User;
import com.example.commoditymanagementsystemspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * The type User cn.jtvc.service.
 *
 * @author 雷族
 */
@Service()
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * Add int.
     *
     * @param user the user
     * @return the int
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(User user) throws Exception {
        if (user.getName() != null && !"".equals(user.getName())) {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("username", user.getName());
            if (userDao.selectCount(wrapper) > 0) {
                throw new Exception("用户名已存在，请重新创建！");
            }
            return userDao.insert(user);
        }
        throw new Exception("用户名不能为空");
    }

    /**
     * Find user user.
     *
     * @param user the user
     * @return the user
     */
    @Override
    public User find(User user) throws Exception {
        if (user.getName() != null && !"".equals(user.getName()) && user.getPassword() != null && !"".equals(user.getPassword())) {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("username", user.getName());
            wrapper.eq("password", user.getPassword());
            return userDao.selectOne(wrapper);
        }
        throw new Exception("用户名和密码不能为空");
    }

    /**
     * Find all list.
     *
     * @return the list
     */
    @Override
    public List<User> findAll(User user) {
        return userDao.findAll(user);
    }

    /**
     * Find all list.
     *
     * @param creator the creator
     * @return the list
     */
    @Override
    public List<User> findAll(String creator) {
        User user = new User();
        user.setCreator(creator);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("creator", user.getCreator());
        return userDao.selectList(wrapper);
    }

    /**
     * Update int.
     *
     * @param user the user
     * @return the int
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(User user) throws Exception {
        if (user.getName() != null && !"".equals(user.getName())) {
            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
            wrapper.eq("username", user.getName());
            user.setName(null);
            return userDao.update(user, wrapper);
        }
        throw new Exception("用户名不能为空");
    }

    /**
     * Delete int.
     *
     * @param name the name
     * @return the int
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(String name) throws Exception {
        if (name != null && !"".equals(name)) {
            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
            wrapper.eq("username", name);
            return userDao.delete(wrapper);
        }
        throw new Exception("用户名不能为空");
    }

    /**
     * Find by page page bean.
     *
     * @param pageCode the page code
     * @param pageSize the page size
     * @return the page bean
     */
    @Override
    public PageBean findByPage(User user, int pageCode, int pageSize) {

        //查询所有数据
        List<User> userList = userDao.findAll(user);
        List<User> userList2 = new ArrayList<>();
        int index = pageSize * (pageCode - 1);
        //当前数据不支持所选择分页直接显示第一页数据
        if (index >= userList.size()) {
            for (int i = 0; i < pageSize && i < userList.size(); i++) {
                userList2.add(userList.get(i));
            }
            return new PageBean(userList.size(), userList2);
        }
        //分页展示数据
        for (int i = 0; i < pageSize && index + i < userList.size(); i++) {
            userList2.add(userList.get(index + i));
        }
        return new PageBean(userList.size(), userList2);
    }
}
